#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItemModel>
#include <QTableView>
#include <QItemSelectionModel>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QStandardItemModel * model = new QStandardItemModel(7,5,this);
    for(int row = 0;row<7;++row){
        for(int column = 0;column<5;++column){
            QStandardItem * item= new QStandardItem(QString("%1").arg(row*5+column+1));
            model->setItem(row,column,item);
        }
    }
    _table_view = new QTableView(this);
    _table_view->setModel(model);
    _table_view->resize(700,500);
    //setCentralWidget(_table_view);

    QItemSelectionModel * selection_model = _table_view->selectionModel();
    QModelIndex left_top(model->index(1,1,QModelIndex()));
    QModelIndex right_bottom(model->index(3,3,QModelIndex()));
    QItemSelection selection(left_top,right_bottom);
    selection_model->select(selection,QItemSelectionModel::Select);

    QTableView * table_view2 = new QTableView(this);
    table_view2->setModel(model);
    table_view2->setSelectionModel(selection_model);
    table_view2->move(600,25);
    table_view2->resize(700,500);

    ui->toolBar->addAction(tr("当前索引"),this,&MainWindow::getCurrentItem);
    ui->toolBar->addAction(tr("反选"),this,&MainWindow::toggleSelection);
    connect(selection_model, &QItemSelectionModel::selectionChanged, this, &MainWindow::updateSelection);
    //选择模型的当前项目更改后，关联changeCurrent函数
    connect(selection_model, &QItemSelectionModel::currentChanged, this, &MainWindow::changeCurrent);

    _table_view->move(0,25);


}

MainWindow::~MainWindow()
{
    delete ui;
    //delete _table_view;
}

void MainWindow::getCurrentItem()
{
    auto data = _table_view->selectionModel()->currentIndex().data().toString();
    qDebug()<<"当前索引的数据： " << data;
}

void MainWindow::toggleSelection()
{
    auto max_row = _table_view->model()->rowCount();
    auto max_column = _table_view->model()->columnCount();
    QModelIndex right_bottom = _table_view->model()->index(max_row-1,max_column-1,QModelIndex());
    QModelIndex left_top = _table_view->model()->index(0,0,QModelIndex());
    QItemSelection selection(left_top,right_bottom);
    _table_view->selectionModel()->select(selection,QItemSelectionModel::Toggle);
}

void MainWindow::updateSelection(const QItemSelection &selected, const QItemSelection &deselected)
{
    QModelIndex index;
    QModelIndexList list = selected.indexes();
    //为现在的选择项目设置值
    for(int i =0; i < list.size(); i++){
        QString text = QString("(%1, %2)").arg(list[i].row()).arg(list[i].column());
        _table_view->model()->setData(list[i], text);
    }
    list = deselected.indexes();
    foreach(index, list){
        _table_view->model()->setData(index,"");
    }
}

void MainWindow::changeCurrent(const QModelIndex &current, const QModelIndex &previous)
{
    qDebug() << tr("move(%1, %2) to (%3, %4)").arg(previous.row())
    .arg(previous.column()).arg(current.row()).arg(current.column());
}
